import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import TestView from '../views/TestView.vue'
import ICompany from '@/views/company/ICompany.vue'
import CompanyPost from '@/views/company/CampanyPost.vue'
import StudentInfo from '@/views/student/stdentInfo.vue'
import StudentResumeEdit from '@/views/student/studentResumeEdit.vue'
import SendInfo from '@/views/send/SendInfo.vue'
// import store from '@/store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: '/company',
        name: 'company',
        component: ICompany
      },
      {
        path: '/companyPost',
        name: 'companyPost',
        component: CompanyPost
      },
      {
        path: '/studentInfo',
        name: 'student',
        component: StudentInfo
      },
      {
        path: '/studentResumeEdit',
        name: 'studentResumeEdit',
        component: StudentResumeEdit
      },
      {
        path: '/sendInfo',
        name: 'SendInfo',
        component: SendInfo
      }
    ]
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login')
  },
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/Login')
  },
  {
    path: '/test',
    name: 'test',
    component: TestView
  }
]

const router = new VueRouter({
  // 去掉#号，hash模式兼容性好
  mode: 'history',
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path !== '/' && to.path !== '/login') {
    console.log('topath===', to.path)
    const token = JSON.parse(sessionStorage.getItem('token'))
    console.log('token:', token)
    if (token) {
      next()
    } else {
      next('/login')
      Vue.prototype.$message.error('请先登录')
    }
  } else {
    next()
  }
})
export default router
